package com.example.service;

import com.example.config.SparkManager;
import com.example.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service
@Slf4j
public class SparkService {
    @Resource
    public SparkManager sparkManager;

    public List<Question> createChoiceQuestionAi(String userInput, int num) {
        String a = new String();

        List<Question> result=new ArrayList<>();
        Date date = new Date();
        String data = sparkManager.sendMesToAIUseXingHuo_question_choice(userInput);
        Date endDate = new Date();
        long differenceInMillis = endDate.getTime() - date.getTime();
        System.out.println(differenceInMillis);

        // 定义正则表达式来匹配每个部分
        Pattern questionPattern = Pattern.compile("question:\\s*(.+?)(?=\\nquestion_type:)", Pattern.DOTALL);
        Pattern questionTypePattern = Pattern.compile("question_type:\\s*(.+?)(?=\\nanswer)", Pattern.DOTALL);
        Pattern answerPattern = Pattern.compile("answer:\\s*(.+?)(?=\\nquestion:)", Pattern.DOTALL);
        Pattern answerPattern1 = Pattern.compile("answer:\\s*(.+)", Pattern.DOTALL);

        for(int i=1;i<=num-1;i++){
            Question entity=new Question();

            String question = extractData(questionPattern, data);
            String questionType = extractData(questionTypePattern, data);
            String answer = extractData(answerPattern, data);

            entity.setContent(question);
            entity.setType(questionType);
            entity.setAnswer(answer);

            result.add(entity);
            data=extractData(answerPattern1,data);
        }
        Question entity=new Question();
        String question = extractData(questionPattern, data);
        String questionType = extractData(questionTypePattern, data);
        String answer = extractData(answerPattern1, data);

        entity.setContent(question);
        entity.setType(questionType);
        entity.setAnswer(answer);
        result.add(entity);
        return result;
    }

    public List<Question> createQuestionAi(String userInput, int num) {
        List<Question> result=new ArrayList<>();
        String data = sparkManager.sendMesToAIUseXingHuo_question(userInput);
        log.info(data);

        // 定义正则表达式来匹配每个部分
        Pattern questionPattern = Pattern.compile("question:\\s*(.+?)(?=\\nquestion_type:)", Pattern.DOTALL);
        Pattern questionTypePattern = Pattern.compile("question_type:\\s*(.+?)(?=\\nanswer)", Pattern.DOTALL);
        Pattern answerPattern = Pattern.compile("answer:\\s*(.+?)(?=\\nquestion:)", Pattern.DOTALL);
        Pattern answerPattern1 = Pattern.compile("answer:\\s*(.+)", Pattern.DOTALL);

        for(int i=1;i<=num-1;i++){
            Question entity=new Question();

            String question = extractData(questionPattern, data);
            String questionType = extractData(questionTypePattern, data);
            String answer = extractData(answerPattern, data);

            entity.setContent(question);
            entity.setType(questionType);
            entity.setAnswer(answer);

            result.add(entity);
            data=extractData(answerPattern1,data);
        }
        Question entity=new Question();
        String question = extractData(questionPattern, data);
        String questionType = extractData(questionTypePattern, data);
        String answer = extractData(answerPattern1, data);

        entity.setContent(question);
        entity.setType(questionType);
        entity.setAnswer(answer);
        result.add(entity);
        return result;
    }

    public List<Question_Student> correctStuQuestionAi(String userInput, int num) {
        List<Question_Student> questionStudents=new ArrayList<>();
        String data=sparkManager.sendMesToAIUseXingHuo_correct_question(userInput);
        data = data;
        System.out.println(data);

        // 定义正则表达式来匹配每个部分
        Pattern qNumPattern = Pattern.compile("QNum:\\s*(.+?)(?=\\nUAnswer:)", Pattern.DOTALL);
        Pattern questionPattern = Pattern.compile("Question:\\s*(.+?)(?=\\nAnswer:)", Pattern.DOTALL);
        Pattern answerPattern = Pattern.compile("Answer:\\s*(.+?)(?=\\nUAnswer:)", Pattern.DOTALL);
        Pattern uAnswerPattern = Pattern.compile("UAnswer:\\s*(.+?)(?=\\nCorrect:)", Pattern.DOTALL);
        Pattern correctPattern = Pattern.compile("Correct:\\s*(.+?)(?=\\nQNum:)", Pattern.DOTALL);
        Pattern answerPattern1 = Pattern.compile("Correct:\\s*(.+)", Pattern.DOTALL);

        for(int i=1;i<=num-1;i++){
            Question_Student entity=new Question_Student();

            int QNum = Integer.parseInt(extractData(qNumPattern, data));
            String correctAnswer = extractData(correctPattern, data);
            String uAnswer = extractData(uAnswerPattern, data);
            entity.setCorrect(correctAnswer);
            entity.setQid(QNum);
            entity.setUanswer(uAnswer);
            data=extractData(answerPattern1, data);
            System.out.println(entity);
            System.out.println("111111111111111111111111111111111111111111111");
            questionStudents.add(entity);
        }

        Question_Student entity=new Question_Student();
        int QNum = Integer.parseInt(extractData(qNumPattern, data));
        String correctAnswer = extractData(answerPattern1, data);
        String uAnswer = extractData(uAnswerPattern, data);

        entity.setUanswer(uAnswer);
        entity.setCorrect(correctAnswer);
        entity.setQid(QNum);

        questionStudents.add(entity);

        return questionStudents;
    }

    public List<Question_Parent> correctParQuestionAi(String userInput, int num) {
        List<Question_Parent> questionParents=new ArrayList<>();
        String data=sparkManager.sendMesToAIUseXingHuo_correct_question(userInput);
        data = data;
        System.out.println(data);

        // 定义正则表达式来匹配每个部分
        Pattern qNumPattern = Pattern.compile("QNum:\\s*(.+?)(?=\\nUAnswer:)", Pattern.DOTALL);
        Pattern questionPattern = Pattern.compile("Question:\\s*(.+?)(?=\\nAnswer:)", Pattern.DOTALL);
        Pattern answerPattern = Pattern.compile("Answer:\\s*(.+?)(?=\\nUAnswer:)", Pattern.DOTALL);
        Pattern uAnswerPattern = Pattern.compile("UAnswer:\\s*(.+?)(?=\\nCorrect:)", Pattern.DOTALL);
        Pattern correctPattern = Pattern.compile("Correct:\\s*(.+?)(?=\\nQNum:)", Pattern.DOTALL);
        Pattern answerPattern1 = Pattern.compile("Correct:\\s*(.+)", Pattern.DOTALL);

        for(int i=1;i<=num-1;i++){
            Question_Parent entity=new Question_Parent();

            int QNum = Integer.parseInt(extractData(qNumPattern, data));
            String correctAnswer = extractData(correctPattern, data);
            String uAnswer = extractData(uAnswerPattern, data);
            entity.setCorrect(correctAnswer);
            entity.setQid(QNum);
            entity.setUanswer(uAnswer);
            data=extractData(answerPattern1, data);
            System.out.println(entity);
            System.out.println("111111111111111111111111111111111111111111111");
            questionParents.add(entity);
        }

        Question_Parent entity=new Question_Parent();
        int QNum = Integer.parseInt(extractData(qNumPattern, data));
        String correctAnswer = extractData(answerPattern1, data);
        String uAnswer = extractData(uAnswerPattern, data);

        entity.setUanswer(uAnswer);
        entity.setCorrect(correctAnswer);
        entity.setQid(QNum);

        questionParents.add(entity);

        return questionParents;
    }

    public List<Question_Teacher> correctTeaQuestionAi(String userInput, int num) {
        List<Question_Teacher> questionTeachers=new ArrayList<>();
        String data=sparkManager.sendMesToAIUseXingHuo_correct_question(userInput);
        data = data;
        System.out.println(data);

        // 定义正则表达式来匹配每个部分
        Pattern qNumPattern = Pattern.compile("QNum:\\s*(.+?)(?=\\nUAnswer:)", Pattern.DOTALL);
        Pattern questionPattern = Pattern.compile("Question:\\s*(.+?)(?=\\nAnswer:)", Pattern.DOTALL);
        Pattern answerPattern = Pattern.compile("Answer:\\s*(.+?)(?=\\nUAnswer:)", Pattern.DOTALL);
        Pattern uAnswerPattern = Pattern.compile("UAnswer:\\s*(.+?)(?=\\nCorrect:)", Pattern.DOTALL);
        Pattern correctPattern = Pattern.compile("Correct:\\s*(.+?)(?=\\nQNum:)", Pattern.DOTALL);
        Pattern answerPattern1 = Pattern.compile("Correct:\\s*(.+)", Pattern.DOTALL);

        for(int i=1;i<=num-1;i++){
            Question_Teacher entity=new Question_Teacher();

            int QNum = Integer.parseInt(extractData(qNumPattern, data));
            String correctAnswer = extractData(correctPattern, data);
            String uAnswer = extractData(uAnswerPattern, data);
            entity.setCorrect(correctAnswer);
            entity.setId(QNum);
            entity.setUanswer(uAnswer);
            data=extractData(answerPattern1, data);
            System.out.println(entity);
            System.out.println("111111111111111111111111111111111111111111111");
            questionTeachers.add(entity);
        }

        Question_Teacher entity=new Question_Teacher();
        int QNum = Integer.parseInt(extractData(qNumPattern, data));
        String correctAnswer = extractData(answerPattern1, data);
        String uAnswer = extractData(uAnswerPattern, data);

        entity.setUanswer(uAnswer);
        entity.setCorrect(correctAnswer);
        entity.setId(QNum);

        questionTeachers.add(entity);

        return questionTeachers;
    }

    public String correctAnalysisSubject(String userInput) {
        String data=sparkManager.sendMesToAIUseXingHuo_create_analysis(userInput);
        System.out.println(data);
        return data;
    }

    public String questionContent(String userInput){
        String data = sparkManager.sendMesToAIUseXingHuo_create_content(userInput);
        Pattern answerPattern1 = Pattern.compile("回答:\\s*(.+)", Pattern.DOTALL);
        String correctAnswer = extractData(answerPattern1, data);
        return correctAnswer;
    }


    // 提取匹配数据的辅助方法
    private static String extractData(Pattern pattern, String data) {
        Matcher matcher = pattern.matcher(data);
        if (matcher.find()) {
            return matcher.group(1).trim();
        }
        return null;
    }

}
